/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.fi.muni.diplomka.common.enumeration.ResearchType;
import cz.muni.fi.diplomka.datalayer.dao.ResearchDao;
import cz.muni.fi.diplomka.datalayer.entity.ResearchEntity;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jakub Kutil
 */
public class ResearchDaoImpl implements ResearchDao {

    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public ResearchDaoImpl() {
    }

    @Override
    public void addResearch(ResearchEntity researchEntity) {
        if (researchEntity == null) {
            throw new IllegalArgumentException("ResearchEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String insert = "INSERT INTO research"
                    + "(player_id, name) VALUES"
                    + "(?,?)";
            ps = con.prepareStatement(insert);

            ps.setLong(1, researchEntity.getPlayerId());
            ps.setString(2, researchEntity.getResearchType().toString());

            ps.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(ResearchDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void editResearch(ResearchEntity researchEntity) {
        if (researchEntity == null) {
            throw new IllegalArgumentException("ResearchEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String update = "UPDATE research SET name=? WHERE  player_id=?";

            ps = con.prepareStatement(update);

            ps.setString(1, researchEntity.getResearchType().toString());
            ps.setLong(2, researchEntity.getPlayerId());

            ps.executeUpdate();
            
        } catch (SQLException ex) {
            Logger.getLogger(ResearchDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public List<ResearchEntity> getAllResearchesOfPlayer(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("Player ID can not be null");
        }
        List<ResearchEntity> getAllResearchList = new ArrayList<ResearchEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllResearchOfPlayer = "SELECT * FROM research WHERE player_id=?";

            ps = con.prepareStatement(getAllResearchOfPlayer);
            ps.setLong(1, playerId);

            rs = ps.executeQuery();

            while (rs.next()) {
                ResearchEntity researchEntity = new ResearchEntity();
                researchEntity.setPlayerId(rs.getLong("player_id"));
                researchEntity.setResearchType(ResearchType.valueOf(rs.getString("name")));

                getAllResearchList.add(researchEntity);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ResearchDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return getAllResearchList;
    }
}
